<!-- subpkg_consult/department/department -->
<script lang="ts" setup>
  import { departmentListApi } from '@/api/consult'
  import type { DepartData } from '@/types/consult'
  import { onLoad } from '@dcloudio/uni-app'
  import { computed, ref } from 'vue'

  const depList = ref<DepartData[]>([])
  const activeIndex = ref(0)

  // 3. 参数传递
  defineProps<{
    type: string
    illnessType: string
  }>()

  onLoad(async (params) => {
    const res = await departmentListApi()
    depList.value = res
  })

  // 3. 二级科室的数据
  // 注意一定要加 ?. 因为一开始科室还没请求回来
  const childList = computed(() => {
    return depList.value[activeIndex.value]?.child
  })
</script>

<template>
  <view class="department-page">
    <scroll-view scroll-y class="department-primary">
      <view
        v-for="(item, index) in depList"
        :key="item.id"
        @click="activeIndex = index"
        :class="{ active: activeIndex === index }"
        class="department-item"
        >{{ item.name }}</view
      >
    </scroll-view>
    <scroll-view class="department-secondary">
      <navigator
        v-for="item in childList"
        :key="item.id"
        hover-class="none"
        :url="`/subpkg_consult/description/description?type=${type}&illnessType=${illnessType}&depId=${item.id}`"
        class="department-item"
      >
        {{ item.name }}
      </navigator>
    </scroll-view>
  </view>
</template>

<style lang="scss">
  @import './department.scss';
</style>
